import upload from '@dfeidao/atom-mobile/fs/upload';
import camera from '@dfeidao/atom-mobile/image-crop-picker/camera';
import { IFeidaoAiMobile } from '@dfeidao/atom-mobile/interfaces';
import get from '@dfeidao/atom-mobile/local/get';
import nodejs from '@dfeidao/atom-mobile/msg/nodejs';
import service from '@dfeidao/atom-mobile/msg/service';
import alert from '@dfeidao/atom-mobile/msgbox/alert';
import toast from '@dfeidao/atom-mobile/msgbox/toast';
import render from '@dfeidao/atom-mobile/render/render';
import { file_url, productid, secretId, secretKey, spaceid, systemid } from '../atom/config';
import remove_file from '../atom/remove-file';

export default async function a002(fd: IFeidaoAiMobile) {
	// 点击“替换照片”按钮
	try {
		// 打开相机+上传图片
		const img = await camera({
			mediaType: 'photo',
			compressImageQuality: 0.5
		}) as { mime: string; size: number; path: string };
		if (img.mime.indexOf('image') === -1) {
			console.log('只支持图片格式！');
			return;
		}
		if (img.size > 1024 * 1024 * 5) {
			console.log('图片不可大于5MB！');
			return;
		}
		// render(fd, { loading: '0' });
		const files = [
			{
				name: '',
				filename: img.mime.replace('image/', ''),
				filepath: img.path,
				filetype: img.mime
			}
		];
		const d = get(fd, 'd') as {
			productid: string; student_no: string; headimg: string;
		};
		const r = await upload(fd, {
			files,
			toUrl: `${file_url}/upload?productid=${productid}`,
			method: 'POST',
			headers: {
				'Accept': 'application/json'
			}
		}, 'a003', 'a004') as { code: number, msg: [{ _id: string, filename: string }] };
		render(fd, { loading: '1' });
		console.log(files, 'r--->', r);
		if (!r.code) {
			console.log(r.msg);
			return alert('提示', '上传失败！', ['确定']);
		}
		// sucfaceids	人脸id
		// 调用创建人脸接口
		const faceid = r.msg[0].filename;
		const r1 = await service<{
			SucFaceIds: string;
			msg: string;
			code: 1 | 0
		}>('thirdpart.tencentcloud.createpersonface', {
			productid,
			spaceid,
			systemid,
			secretId,
			secretKey,
			PersonId: d.student_no,
			fileurl: file_url.replace('/fsweb', ''),
			fileid: faceid
		});
		console.log('r1--->', r1);
		if (!r1.code) {
			return alert('提示', r1.msg, ['确定']);
		}

		// 修改<人脸图库>表
		const sucfaceids = r1.SucFaceIds;
		const r2 = await nodejs<{ code: number; msg: string; sucfaceids: string; faceid: string; }>('app-replac-photo/s002', {
			params: {
				student_no: d.student_no,
				productid: d.productid,
				faceid,
				sucfaceids
			}
		});
		if (!r2.code) {
			return alert('提示', r2.msg, ['确定']);
		}

		// 删除原人脸(调用删除人脸接口)
		const del_sucfaceids = r2.sucfaceids;
		const r3 = await service<{
			msg: string;
			code: 1 | 0
		}>('thirdpart.tencentcloud.delpersonface', {
			productid,
			spaceid,
			systemid,
			secretId,
			secretKey,
			PersonId: d.student_no,
			faceIds: [del_sucfaceids]
		});
		console.log('r3--->', r3);
		if (!r3.code) {
			alert('提示', r3.msg, ['确定']);
		}

		// 删除原图片
		const del_faceid = r2.faceid;
		const r4 = await remove_file(file_url, productid, [del_faceid]);
		console.log('r4--->', r4);
		if (!r4.code) {
			alert('提示', '删除原图片失败', ['确定']);
		}
		render(fd, { headimg: file_url + `/getfile?productid=${productid}&id=${faceid}` });
	} catch (err) {
		console.log(err);
		if (err.indexOf('图片中没有人脸') === -1) {
			toast({
				type: 'danger',
				text: '图片中没有人脸',
				position: 'top'
			});
		} else {
			toast({
				type: 'danger',
				text: '上传失败',
				position: 'top'
			});
		}
	}
}
